/* GWFile.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-2-18	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.imp;

import java.io.DataInputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.base.Product;
import system.base.WarehouseCategory;
import framework.core.DBConnection;
import framework.core.ILoginContext;

public class GWFile {
    public static String batchImportManufacture(DataInputStream aFile, DBConnection con, ILoginContext loginContext) throws Exception 
    {
        int tSuccCount = 0;
        int tFailCount = 0;
        String tFailFile = "0";
        HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
        HSSFSheet tHSSFSheet = null;
        try{
            //开始读取excel文件
            tHSSFWorkbook = new HSSFWorkbook(aFile);           //读取sheet表单
            //读取sheet表单
            tHSSFSheet = tHSSFWorkbook.getSheetAt(0);
        }catch(Exception e){
            e.printStackTrace();
            //LogWriter.error(e.getMessage());
           // throw new GWException("批量上传文件失败");
            throw new Exception("上传文件失败");
        }
            ArrayList tList = new ArrayList();
            ArrayList tErrList = new ArrayList();
            ArrayList tSuccList = new ArrayList();
            //读取并检查明细
           int tRowCount = 0;//getManufactureContents(tList,tHSSFSheet, loginContext);
            //GSManufactureDAO tGSManufactureDAO=new GSManufactureDAO();
            if(tList.size()>0){
                
                WarehouseCategory wc = new WarehouseCategory();
                
                
                
            }
            
            
            
            if(tList.size() > 0){
                for(int i=0; i<tList.size(); i++){
                    GWManufactureUploadVO tGSManufactureUploadVO = (GWManufactureUploadVO)tList.get(i);
                    Product product = tGSManufactureUploadVO.getProduct();

                    int tResult = Integer.parseInt(tGSManufactureUploadVO.getInsert_result());
                    if(tResult == 1){
                        tGSManufactureUploadVO.setProduct(product);
                       try{
                           //tGSManufactureBean.setGs_is_tradeflat_manufacture("1");
                           //tGSManufactureBean.setGs_recorder_id(aUserName);
                           //String tSetManufactureFilter = GSSystemConfigurator.getParam("gs.SetManufactureFilter");
                           
                           //判断是否存在产品信息
                           Product obj = Product.fetchByConcentricityCode(con, product.getConcentricityCode());
                           if(obj == null) obj = Product.fetchByApepCode(con, product.getApepCode());
                           if(obj != null) product.setId(obj.getId());
                           product.save(con);
                           
                           /*if(new GSManufactureDAO().isExist(tGSManufactureBean.getGs_manufacture_id()))
                           {
                                tGSManufactureDAO.modify(tGSManufactureBean);   
                           }
                           else
                           {
                                //产品导入是否需要筛选                                
                                boolean isAdd = true;
                                if (tSetManufactureFilter.equalsIgnoreCase("true"))
                                {
                                    if (!(new GSManufactureFilterUploadDAO()).isExist(tGSManufactureBean.getGs_manufacture_id()))
                                    {
                                        isAdd = false;
                                    }       
                                }
                                
                                //新增产品信息
                                if (isAdd) tGSManufactureDAO.add(tGSManufactureBean);
                                
                           }*/
                           tSuccCount ++;
                           //System.out.println(tSuccCount+"记录");
                           tGSManufactureUploadVO.setInsert_result("1");
                           tSuccList.add(tGSManufactureUploadVO);
                       }catch(Exception e){
                           tFailCount++;
                           tGSManufactureUploadVO.setInsert_result("0");
                           tGSManufactureUploadVO.setFailure_reason(e.getMessage());
                           tErrList.add(tGSManufactureUploadVO);
                       }
                    }else if(tResult == 0){
                        tFailCount++;
                        tErrList.add(tGSManufactureUploadVO);
                    }
                    
                }
            }
            //为更新的产品表同步产品过滤表
            //int value = tGSManufactureDAO.filter();
            //将新增失败的商户信息写入文件
            if(tErrList.size() > 0){
                tFailFile = "0";//writeManufactureFile(tErrList,tSuccList,tHSSFWorkbook,tRowCount);
            }
            return tSuccCount+"-"+tFailCount+"-"+tFailFile;
        }

}
